package com.xingkeduo.business.order.remote;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xingkeduo.business.order.remote.feign.AdminFeignService;
import com.xingkeduo.dto.ConsumerStatisticsDTO;
import com.xingkeduo.dto.CraftsmanDTO;
import com.xingkeduo.dto.CraftsmanServiceTO;
import com.xingkeduo.dto.WxMaUserInfo;
import com.xingkeduo.pojo.table.OrderDetail;
import com.xingkeduo.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;


@Service
@Slf4j
public class AdminRemoteService {

    @Resource
    private AdminFeignService adminFeignService;

    @Cacheable(value = "cache:order:getAdmin", key = "#id")
    public CraftsmanDTO getAdmin(String id) {
        log.info("getAdmin:{}", id);
        JSONObject object = new JSONObject();
        object.put("id", id);
        ResponseUtils<CraftsmanDTO> craftsman = adminFeignService.getCraftsman(object.toString());
        log.info("getAdmin response:{}", craftsman);
        if(craftsman !=null && craftsman.success()){
            return craftsman.getData();
        }
        return null;
    }



    /**
     * 获取发型师配价
     * @param shopId
     * @param serviceId
     * @param craftsmanId
     * @return
     */
    public CraftsmanServiceTO listCraftsmanPriceByShopIdAndServiceId(Long shopId, Long serviceId , Long craftsmanId){
        List<CraftsmanServiceTO> craftsmanServiceTOList = null;
        ResponseUtils responseUtils = adminFeignService.listCraftsmanPriceByShopIdAndServiceId( shopId,  serviceId ,  craftsmanId);
        log.info("listCraftsmanPriceByShopIdAndServiceId:{}", responseUtils);
        if(responseUtils!=null && responseUtils.success()) {
            craftsmanServiceTOList = JSON.parseArray(JSON.toJSONString(responseUtils.getData()), CraftsmanServiceTO.class);
            if(craftsmanServiceTOList!=null && craftsmanServiceTOList.size()>0) {
                CraftsmanServiceTO craftsmanServiceTO = craftsmanServiceTOList.get(0);
                return craftsmanServiceTO;
            }
        }
        return null;
    }


    /**
     * 获取用户ids
     * @param userId
     * @return
     */
    @Cacheable(value = "cache:order:queryUserIds", key = "#userId")
    public String[]  queryUserIds(String userId) {
        log.info("queryUserIds:{}", userId);
        List<Object>  list=new ArrayList<>();

        String userListStr = adminFeignService.queryUserListByUserId("{\"id\":" + userId + "}");

        log.info("queryUserIds userids:{}",  JSON.toJSONString(userListStr));
        if (StringUtils.isNotBlank(userListStr)) {
            JSONObject message = JSON.parseObject(userListStr);
            if ("1".equals(message.getOrDefault("status", "0").toString())) {
                list = message.getObject("data", List.class);
            }
        }
        if(list.isEmpty()){
            list.add(userId);
        }
        log.info("list:{}", userId);
        String[] arrays = list.stream().map(s->s+"").toArray(String[]::new);
        return arrays;
    }


    //@Cacheable(value = "cache:order:getConsumerInfo", key = "#userId")
    public ResponseUtils<WxMaUserInfo> getConsumerInfo(String userId){
        log.info("getConsumerInfo:{}", userId);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("id",userId);

        ResponseUtils<WxMaUserInfo> consumerInfo = adminFeignService.getConsumerInfo(JSON.toJSONString(map));

        return consumerInfo;
    }


    /**
    * @description: 获取新老用户信息
    * @author: ch
    * @Param [userids]
    * @return java.util.Map<java.lang.String,java.lang.String>
    * @date: 2019-07-17 17:47
    **/
    public List<ConsumerStatisticsDTO> queryUserOrderSource(List<ConsumerStatisticsDTO>  longs){
        log.info("获取新老用户信息longs:{}", JSON.toJSONString(longs));
        ResponseUtils<List<ConsumerStatisticsDTO>> responseUtils = adminFeignService.getStatistics(longs);
        log.info("queryUserOrderSource:{}", JSON.toJSONString(responseUtils));
        if(responseUtils!=null && responseUtils.success() &&  responseUtils.getData()!=null) {
            List<ConsumerStatisticsDTO> data = responseUtils.getData();
            if(!data.isEmpty()){
                return data;
            }
        }
        List<ConsumerStatisticsDTO> list = new ArrayList<>();
        return list;
    }


}
